package com.jeesite.modules.dataScreen.web;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jeesite.modules.dataScreen.dao.IndexsDao;
import com.jeesite.modules.dataScreen.dao.JtdnZbDao;
import com.jeesite.modules.dataScreen.dao.Test1Dao;
import com.jeesite.modules.dataScreen.dao.TianbaoDao;
import com.jeesite.modules.dataScreen.entity.Indexs;
import com.jeesite.modules.dataScreen.entity.JtdnZb;
import com.jeesite.modules.dataScreen.entity.Test1;
import com.jeesite.modules.dataScreen.entity.Tianbao;
import com.jeesite.modules.dataScreen.web.dto.AddIndexParam;
import com.jeesite.modules.dataScreen.web.vo.Result;
import com.mysql.cj.x.protobuf.Mysqlx;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author: zhaosenlin
 * @date: 2024/5/14 14:10
 */
@RestController
@Api("其他接口")
@RequestMapping("/other")
@Slf4j
public class OtherApi {
    // @Resource
    // IndexsDao  indexsDao;
    // @Resource
    // ZhDataDao indexsDao;
    @Resource
    JtdnZbDao jtdnZbDao;
    @Resource
    TianbaoDao tianbaoDao;
    @Resource
    Test1Dao test1Dao;

    @ApiOperation("添加指标信息")
    @PostMapping("batchUpdateTest1")
    public Result<?> batchUpdateTest1(@RequestBody List<JSONObject> tianbao) {
        for (JSONObject tbJson : tianbao) {
            String indexName = tbJson.getString("index_name");
            Assert.notBlank(indexName, "IndexName 不能为空");

            Test1 test1 = test1Dao.selectOne(Wrappers.<Test1>lambdaQuery()
                                                     .eq(Test1::getIndexName, indexName).last("limit 1"));
            Test1 newtest1 = new Test1();
            newtest1.setDanwei(tbJson.getString("danwei"));
            newtest1.setTbdanwei(tbJson.getString("tbdanwei"));
            newtest1.setUpdate(tbJson.getString("update"));
            newtest1.setIndexName(indexName);

            if (test1 == null) {
                Assert.notBlank(newtest1.getDanwei(), "Danwei 不能为空");
                Assert.notBlank(newtest1.getTbdanwei(), "Tbdanwei 不能为空");
                Assert.notBlank(newtest1.getUpdate(), "Update 不能为空");
                test1Dao.insert(newtest1);
            } else {
                test1Dao.update(newtest1, Wrappers.<Test1>lambdaQuery().eq(Test1::getIndexName, indexName));
            }
        }
        return Result.OK();
    }

    @ApiOperation("更新填报")
    @PostMapping("batchUpdateIndex")
    public Result<?> batchUpdateIndex(@RequestBody List<JSONObject> tianbao) {
        for (JSONObject tbJson : tianbao) {
            String indexName = tbJson.getString("index_name");
            Test1 test1 = test1Dao.selectOne(Wrappers.<Test1>lambdaQuery()
                                                     .eq(Test1::getIndexName, indexName).last("limit 1"));

            if (test1 == null) {
                throw new IllegalArgumentException("没有指标信息" + indexName);
            }

            Tianbao tb = new Tianbao();
            tb.setUpdate(test1.getUpdate());
            tb.setTbdw(test1.getTbdanwei());
            tb.setIndexValue(tbJson.getString("index_value"));
            tb.setIndexName(indexName);
            updateIndex(tb);
        }
        return Result.OK();
    }

    @ApiOperation("更新填报")
    @PostMapping("updateIndex")
    public Result<?> updateIndex(@RequestBody Tianbao tianbao) {
        tianbao.setTbdate(DateUtil.date());
        tianbaoDao.insert(tianbao);
        return Result.OK();
    }

    @ApiOperation("添加指标2")
    @PostMapping("addIndex2")
    public Result<?> addIndex2(@RequestBody AddIndexParam param) {
        Assert.notBlank(param.getIndexName());

        JtdnZb zhData = new JtdnZb();
        zhData.setZbmc(param.getIndexName());
        zhData.setSjzWb(param.getExampleValue());
        zhData.setSjdw(param.getUnit());

        JtdnZb db = jtdnZbDao.selectOne(Wrappers.<JtdnZb>lambdaQuery().eq(JtdnZb::getZbmc, param.getIndexName()));
        if (db != null) {
            zhData.setId(db.getId());
            jtdnZbDao.updateById(zhData);
        } else {
            jtdnZbDao.insert(zhData);
        }

        return Result.OK();
    }

    // @ApiOperation("list")
    // @GetMapping("list")
    // public Result<?> list() {
    //     List<Indexs> indexsList = indexsDao.selectList(Wrappers.emptyWrapper());
    //     return Result.OK(indexsList);
    // }
    //
    // @ApiOperation("添加指标")
    // @PostMapping("addIndex")
    // public Result<?> addIndex(@RequestBody AddIndexParam param) {
    //     String description = StrUtil.format("{}_{}", param.getModelName(), param.getIndexName());
    //
    //     if (StrUtil.isBlank(param.getKey())) {
    //         param.setKey(PinyinUtil.getFirstLetter(description, ""));
    //     }
    //
    //     Indexs zhData = new Indexs();
    //     zhData.setName(param.getKey());
    //     zhData.setDescription(description);
    //     zhData.setStaticValue(param.getExampleValue());
    //
    //     Indexs db = indexsDao.selectOne(Wrappers.<Indexs>lambdaQuery().eq(Indexs::getName, param.getKey()));
    //     if (db != null) {
    //         zhData.setId(db.getId());
    //         indexsDao.updateById(zhData);
    //     } else {
    //         indexsDao.insert(zhData);
    //     }
    //
    //     return Result.OK();
    // }

    // @ApiOperation("批量添加指标")
    // @PostMapping("batchAddIndex")
    // public Result<?> batchAddIndex(@RequestBody List<AddIndexParam> param) {
    //     if (param != null) {
    //         for (AddIndexParam addIndexParam : param) {
    //             Result<?> result = addIndex(addIndexParam);
    //             if (!result.isSuccess()) {
    //                 return result;
    //             }
    //         }
    //     }
    //     return Result.OK();
    // }
}
